package com.fivefu.core.skyeye.event.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.base.web.controller.BaseController;
import com.fivefu.base.web.vo.ResultInfo;
import com.fivefu.core.skyeye.event.services.SceneService;
import com.fivefu.core.skyeye.event.utils.VerifyDataUtils;
import com.fivefu.core.skyeye.event.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 探头场景关联表 前端控制器
 * </p>
 *
 * @author 
 * @since 2022-07-15 13:53:19
 */
@Api(tags = "场景探头")
@RestController
@RequestMapping("/sceneCamera")
public class DbAiSceneCameraController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(DbAiSceneCameraController.class);

    @Autowired
    private SceneService sceneService;

    @RequestMapping(path = "/editSceneCamera",method = RequestMethod.POST)
    @ApiOperation(value = "编辑场景探头",httpMethod = "POST",response = ResultInfo.class,notes = "编辑场景探头")
    public ResultInfo editScene(@ModelAttribute @Valid SceneCameraVo sceneCameraVo, @RequestAttribute String userid){
        try {
            logger.info("编辑场景探头信息接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            String value = VerifyDataUtils.verifyRequiredColumn(sceneCameraVo);
            if (StrUtils.isNotNull(value)){
                return ResultInfo.renderError(value + "不能为空");
            }
            //验证探头有效时间格式
            if (StrUtils.isNotNull(sceneCameraVo.getCameraValidTime())){
                try {
                    Object timeArr[] = (Object[]) VerifyDataUtils.verifyJSONArray(sceneCameraVo.getCameraValidTime());
                    for (int i = 0 ; i < timeArr.length ; i ++){
                        Object arr[] = (Object[]) timeArr[i];
                        for (int j = 0 ; j < arr.length ; j ++){
                            String time = (String) arr[j];
                            VerifyDataUtils.verifyDateTime(time, "HHmm");
                        }
                    }
                }catch (Exception e){
                    return ResultInfo.renderError("探头有效时间格式不正确");
                }
            }
            boolean flag = sceneService.editSceneCamera(sceneCameraVo,userid);
            if (flag){
                return ResultInfo.renderSuccess("编辑场景探头成功");
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("编辑场景探头失败");
    }

    @RequestMapping(path = "/enableSceneCamera",method = RequestMethod.POST)
    @ApiOperation(value = "开启、关闭场景探头",httpMethod = "POST",response = ResultInfo.class,notes = "开启、关闭场景探头")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "场景探头编号", paramType = "query",required = true, dataType = "Long"),
            @ApiImplicitParam(name = "enable", value = "是否启用(0:否 1:是)", paramType = "query",required = true, dataType = "int"),
    })
    public ResultInfo enableSceneCamera(Long id,Integer enable, @RequestAttribute String userid){
        try {
            logger.info("开启、关闭场景探头接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            //判断场景探头编号是否为空
            if (id == null){
                return ResultInfo.renderError("场景探头编号不能为空");
            }
            //判断是否启用是否为空
            if (enable == null){
                return ResultInfo.renderError("是否启用不能为空");
            }
            sceneService.enableSceneCamera(id,userid,enable);
            return ResultInfo.renderSuccess("开启、关闭场景探头成功");
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("开启、关闭场景探头失败");
    }

    @RequestMapping(path = "/deleteSceneCamera",method = RequestMethod.POST)
    @ApiOperation(value = "删除场景探头",httpMethod = "POST",response = ResultInfo.class,notes = "删除场景探头")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "场景探头编号", paramType = "query",required = true, dataType = "Long"),
    })
    public ResultInfo deleteSceneCamera(Long id, @RequestAttribute String userid){
        try {
            logger.info("删除场景探头接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            //判断场景探头编号是否为空
            if (id == null){
                return ResultInfo.renderError("场景探头编号不能为空");
            }
            sceneService.deleteSceneCamera(id,userid);
            return ResultInfo.renderSuccess("删除场景探头成功");
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("删除场景探头失败");
    }

    @RequestMapping(path = "/querySceneCameraDetail",method = RequestMethod.POST)
    @ApiOperation(value = "获取场景探头详情",httpMethod = "POST",response = ResultInfo.class,notes = "获取场景探头详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sceneId", value = "场景唯一标识", paramType = "query",required = true, dataType = "String"),
            @ApiImplicitParam(name = "cameraId", value = "探头唯一标识", paramType = "query",required = true, dataType = "String"),
    })
    public ResultInfo querySceneCameraDetail(String sceneId, String cameraId){
        try {
            logger.info("获取场景探头详情接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            //判断场景探头编号是否为空
            if (StrUtils.isNull(sceneId)){
                return ResultInfo.renderError("场景编号不能为空");
            }
            if (StrUtils.isNull(cameraId)){
                return ResultInfo.renderError("探头编号不能为空");
            }
            Map<String, Object> scene = sceneService.querySceneCameraDetail(sceneId, cameraId);
            return ResultInfo.renderSuccess(scene);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("获取场景探头详情失败");
    }

    @RequestMapping(path = "/querySceneCameraByPage",method = RequestMethod.POST)
    @ApiOperation(value = "分页获取场景探头列表",httpMethod = "POST",response = ResultInfo.class,notes = "分页获取场景探头列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cameraCode",value = "场景唯一标识",paramType = "query",required = true, dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页", paramType = "query",required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "每页显示条数", paramType = "query",required = true, dataType = "int")
    })
    public ResultInfo querySceneCameraByPage(String cameraCode,Integer page,Integer limit){
        try {
            logger.info("分页获取场景探头列表接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            //判断场景唯一标识是否为空
            if (StrUtils.isNull(cameraCode)){
                return ResultInfo.renderError("场景探头不能为空");
            }
            //判断当前页是否为空
            if (page==null){
                return ResultInfo.renderError("当前页不能为空");
            }
            //判断每页显示条数是否为空
            if (limit==null){
                return ResultInfo.renderError("每页显示条数不能为空");
            }
            Page<Map<String, Object>> list = sceneService.querySceneCameraByPage(cameraCode,page, limit);
            return ResultInfo.renderSuccess(list);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("分页获取场景探头列表失败");
    }

    @RequestMapping(path = "/querySceneNodeCount",method = RequestMethod.POST)
    @ApiOperation(value = "查询场景探头的机器节点数量",httpMethod = "POST",response = ResultInfo.class,notes = "查询场景探头的机器节点数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "场景探头编号", paramType = "query",required = true, dataType = "Long")
    })
    public ResultInfo querySceneNodeCount(Long id){
        try {
            logger.info("查询场景探头的机器节点数量接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            //判断场景探头编号是否为空
            if (id == null){
                return ResultInfo.renderError("场景探头编号不能为空");
            }
            Map<String, Object> map = sceneService.querySceneNodeCount(id);
            return ResultInfo.renderSuccess(map);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("查询场景探头的机器节点数量失败");
    }

 /*   @RequestMapping(path = "/onOffSceneCamera",method = RequestMethod.POST)
    @ApiOperation(value = "开关场景探头",httpMethod = "POST",response = ResultInfo.class,notes = "开关场景探头")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sceneCode", value = "场景唯一标识", paramType = "query",required = true, dataType = "String"),
            @ApiImplicitParam(name = "cameraList", value = "探头唯一标识(多个逗号分隔)", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "enable", value = "是否开启(0:否 1:是)", paramType = "query",required = true, dataType = "int")
    })
    public ResultInfo onOffSceneCamera(String sceneCode,String cameraList,Integer enable){
        try {
            logger.info("开关场景探头接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            //判断场景唯一标识是否为空
            if (StrUtils.isNull(sceneCode)){
                return ResultInfo.renderError("场景唯一标识不能为空");
            }
            //判断是否开启是否为空
            if (enable == null){
                return ResultInfo.renderError("是否开启不能为空");
            }
            List<String> cameras = new ArrayList<>();
            if (StrUtils.isNotNull(cameraList)){
                cameras  = Arrays.asList(cameraList.split(","));
            }
            sceneService.onOffSceneCamera(sceneCode,cameras,enable);
            return ResultInfo.renderSuccess("开关场景探头成功");
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("开关场景探头失败");
    }*/
}
